<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="设备名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.equipmentName"
                placeholder="请输入设备名称"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="设备编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.equipmentNumber"
                placeholder="请输入设备编号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch('equipmentType')"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col :span="22">
          <a-button type="primary" @click="handleType">新建设备类型</a-button>
          <a-button type="primary" @click="handleAdd">新建设备</a-button>
          <!-- <a-button
            type="primary"
            @click="handelExport('设备管理', 'equipmentListPage', columns)"
            >导出</a-button
          > -->
          <!-- <a-button type="primary"  @click="handleAdd"
            >导入</a-button
          > -->
        </a-col>
        <a-col :span="2">
          <a-button type="primary" @click="handleAllList('equipmentListPage')"
            >全部设备</a-button
          >
        </a-col>
      </a-row>
      <br />
      <a-row :gutter="16">
        <a-col :span="5">
          <div class="treeBox">
            <div class="tree-title">设备类型</div>
            <a-tree
              :tree-data="treeData"
              :show-line="true"
              :selectedKeys="selectedKeys"
              @select="onSelect"
              :replaceFields="{
                children: 'equipmentTypeList',
                title: 'equipmentTypeName',
                key: 'id',
              }"
              v-if="treeData.length > 0"
            >
              <template slot="custom" slot-scope="record">
                <div class="flex flex-a-c flex-j-s-b">
                  <div>{{ record.equipmentTypeName }}</div>
                  <div>
                    <a-icon
                      type="edit"
                      class="icon-edit"
                      @click="handleTypeEdit(record)"
                    />
                    <a-popconfirm
                      okText="确定"
                      cancelText="取消"
                      v-hasPromise="'equipmentDel'"
                      title="确定删除吗?"
                      @confirm="handleTypeDelete(record)"
                    >
                      <a-icon type="delete" class="icon-delete" />
                    </a-popconfirm>
                  </div>
                </div>
              </template>
            </a-tree>
          </div>
        </a-col>
        <a-col :span="19">
          <a-table
            :columns="columns"
            :data-source="tableData"
            :loading="loading"
            :pagination="pagination"
            @change="handleTableChange"
            bordered
            :rowKey="(record, index) => record.id"
          >
            <template slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" v-hasPromise="'equipmentDel'" />
              <a-popconfirm
                v-hasPromise="'equipmentDel'"
                okText="确定"
                cancelText="取消"
                title="确定删除吗?"
                @confirm="() => handleDelete(record)"
              >
                <a>删除</a>
              </a-popconfirm>
              <a-divider type="vertical" />
              <a-icon
                type="qrcode"
                @click.stop="handleCode(record)"
                style="fontSize: 16px;color: #40a9ff;"
              />
            </template>
          </a-table>
        </a-col>
      </a-row>
    </a-card>

    <EquipMentModal
      ref="modalForm"
      @ok="modalFormOk"
      :treeData="treeData"
    ></EquipMentModal>

    <EquipTypeModal
      ref="modalType"
      :treeData="treeData"
      @Yes="modalType"
    ></EquipTypeModal>

    <!-- 二维码 -->
    <qrCode
      ref="qrCode"
      :label="qrModel.label"
      :text="qrModel.text"
      :qrSize="320"
    ></qrCode>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import EquipMentModal from "@/components/EquipMentModal.vue";
import EquipTypeModal from "./components/EquipTypeModal.vue";
import api from "@/api/apiServer";
import { setTreeData } from "@/unit/tool";
import qrCode from "@/components/qrCode";
import { CodeNo } from "@/unit/QRCodeConfig";
export default {
  components: { EquipMentModal, EquipTypeModal, qrCode },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      qrModel: {
        label: "", // 二维码底部内容
        text: "", // 二维码内容
      },
      treeData: [],
    };
  },
  mounted() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    // 获取列表数据
    async getList(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      this.loading = true;
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.equipmentListPage(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
      this.loading = false;
    },
    async getTypeList() {
      const res = await api.equipmentTypeList();
      this.treeData = setTreeData(res.data, "custom", "equipmentTypeList");
      //对树形控件进行插槽设置
    },
    // 删除
    async handleDelete(record) {
      const res = await api.equipmentDelete({
        id: record.id,
      });
      this.getList();
      this.$message.success(res.message);
    },
    // 新增
    handleAdd() {
      this.disabled = false;
      this.$refs.modalForm.title = "新增设备";
      this.$refs.modalForm.modalAdd();
    },
    //  编辑
    handleEdit(record) {
      this.disabled = true;
      this.$refs.modalForm.title = "编辑设备";
      this.$refs.modalForm.modalEdit(record);
    },
    // 客户类型打开弹窗和编辑
    handleType() {
      this.$refs.modalType.title = "新增";
      this.$refs.modalType.modalAdd();
    },
    handleTypeEdit(record) {
      this.$refs.modalType.title = "编辑";
      this.$refs.modalType.modalEdit(record);
    },
    //客户类型删除
    async handleTypeDelete(value) {
      const ids = [];
      ids.push(value.id);
      const params = {
        ids,
      };
      const res = await api.equipmentTypeDelete(params);
      this.$message.success(res.message);
      this.getTypeList();
    },
    modalType() {
      this.getList();
      this.getTypeList();
    },
    onSelect(equipmentType) {
      this.selectedKeys = equipmentType;
      this.queryParam.equipmentType = equipmentType[0];
      this.getList();
    },
    // 生成二维码
    handleCode(record) {
      let { equipmentNumber, equipmentName, equipmentModel, id } = record;
      let newObj = {
        equipmentNumber,
        equipmentName,
        equipmentModel,
        id,
        QRCode: CodeNo.Equipment,
      };
      let text = JSON.stringify(newObj);
      this.$refs.qrCode.title = "设备二维码";
      this.qrModel.label = `${equipmentNumber} ${equipmentName}`;
      this.qrModel.text = text;
      this.$refs.qrCode.list = this.qrModel.label.split(",");
      this.$refs.qrCode.showModal();
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
::v-deep .ant-tree-node-content-wrapper {
  width: 90%;
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
